package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.comprehensive.domain.DustPreventionExamine;
import com.ruoyi.comprehensive.domain.FirePreventionExamine;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.dto.FirePreventionDto;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.service.IFirePreventionExamineService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.FirePreventionMapper;
import com.ruoyi.comprehensive.domain.FirePrevention;
import com.ruoyi.comprehensive.service.IFirePreventionService;

/**
 * 防火管理Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-16
 */
@Service
public class FirePreventionServiceImpl implements IFirePreventionService 
{
    @Autowired
    private FirePreventionMapper firePreventionMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;
    @Autowired
    private IFirePreventionExamineService firePreventionExamineService;

    /**
     * 查询防火管理
     * 
     * @param id 防火管理主键
     * @return 防火管理
     */
    @Override
    public FirePrevention selectFirePreventionById(String id)
    {
        return firePreventionMapper.selectFirePreventionById(id);
    }

    /**
     * 查询防火管理列表
     * 
     * @param firePrevention 防火管理
     * @return 防火管理
     */
    @Override
    public List<FirePrevention> selectFirePreventionList(FirePrevention firePrevention)
    {
        return firePreventionMapper.selectFirePreventionList(firePrevention);
    }

    /**
     * 新增防火管理
     * 
     * @param firePrevention 防火管理
     * @return 结果
     */
    @Override
    public int insertFirePrevention(FirePrevention firePrevention)
    {
        FirePrevention firePrevention1 = new FirePrevention();
        BeanUtils.copyProperties(firePrevention,firePrevention1);
        firePrevention1.setExamineStatus(0L);
        firePrevention1.setCreateByName(SecurityUtils.getUsername());
        firePrevention1.setCreateBy(SecurityUtils.getUserId().toString());
        firePrevention1.setCreateAt(new Date());
        firePrevention1.setUpdateAt(new Date());
        firePrevention1.setUpdateByName(SecurityUtils.getUsername());
        firePrevention1.setUpdateBy(SecurityUtils.getUserId().toString());
        return firePreventionMapper.insertFirePrevention(firePrevention1);
    }

    /**
     * 修改防火管理
     * 
     * @param firePrevention 防火管理
     * @return 结果
     */
    @Override
    public int updateFirePrevention(FirePrevention firePrevention)
    {
        if (firePrevention.getExamineStatus() != null && firePrevention.getExamineStatus() == 1L){
            FirePrevention firePrevention1 = firePreventionMapper.selectFirePreventionById(firePrevention.getId());
            if (firePrevention1 != null){
                FirePreventionExamine firePreventionExamine = new FirePreventionExamine();
                BeanUtils.copyProperties(firePrevention1,firePreventionExamine);
                firePreventionExamine.setFirePreventionId(firePrevention1.getId());
                firePreventionExamine.setExamineStatus(1L);
                firePreventionExamineService.insertFirePreventionExamine(firePreventionExamine);
            }
        }
        return firePreventionMapper.updateFirePrevention(firePrevention);
    }

    /**
     * 批量删除防火管理
     * 
     * @param ids 需要删除的防火管理主键
     * @return 结果
     */
    @Override
    public int deleteFirePreventionByIds(String[] ids)
    {
        return firePreventionMapper.deleteFirePreventionByIds(ids);
    }

    /**
     * 删除防火管理信息
     * 
     * @param id 防火管理主键
     * @return 结果
     */
    @Override
    public int deleteFirePreventionById(String id)
    {
        return firePreventionMapper.deleteFirePreventionById(id);
    }

    /**
     * 获取防火管理列表
     * @param firePrevention
     * @return
     */
    @Override
    public List<FirePreventionDto> getFirePreventionList(FirePrevention firePrevention) {
        List<FirePreventionDto> firePreventionList = firePreventionMapper.getFirePreventionList(firePrevention);
        return firePreventionList;
    }

    /**
     * 获取防火管理详细信息
     * @param id
     * @return
     */
    @Override
    public FirePreventionDto getFirePreventionById(String id) {
        FirePrevention firePrevention = firePreventionMapper.selectFirePreventionById(id);
        FirePreventionDto firePreventionDto = new FirePreventionDto();
        BeanUtils.copyProperties(firePrevention,firePreventionDto);
        PreventionEnclosure preventionEnclosure = new PreventionEnclosure();
        preventionEnclosure.setPreventionId(firePrevention.getId());
        preventionEnclosure.setEnclosureType("1");
        // 查询附件信息内容
        List<PreventionEnclosure> preventionEnclosures = preventionEnclosureMapper.selectPreventionEnclosureList(preventionEnclosure);
        firePreventionDto.setEnclosures(preventionEnclosures);
        return firePreventionDto;
    }
}
